package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;

public class NetActivity extends AppCompatActivity implements Runnable {
    private static final String TAG = "Net";
    Handler handler;
    TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);
        show=findViewById(R.id.net_show);
        handler=new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==5){
                    String str=(String) msg.obj;
                    show.setText(str);
                }
                super.handleMessage(msg);
            }
        };
//        Thread t2=new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
    }
    public void onClick (View btn){
        Thread t=new Thread(this);
        t.start();
    }
    @Override
    public void run() {
        URL url=null;
        String html="";
        try {
       //     url=new URL("https://www.exchange-rates.org/zh/current-rates/cny");
       //     HttpURLConnection http=(HttpURLConnection) url.openConnection();
       //     InputStream in=http.getInputStream();
       //     html=inputStream2String(in);
            Document doc=Jsoup.connect("https://chl.cn/?jinri").get();
            Element table=doc.getElementsByTag("table").first();
            Elements rows=table.getElementsByTag("tr");
            rows.remove(0);
            for(Element row:rows){


                Elements tds=row.getElementsByTag("td");
                Element td1=tds.get();
                Element td2=tds.get(4);
                html+=(td1.text()+"=>"+td2.text()+"\n");
            }
            Element td=doc.select(#Lt > article > div.ahh > table:nth-child(3) > tbody > tr:nth-child(19) > td:nth-child(3))

        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        Message msg=handler.obtainMessage(5,"Hello from thread");
        handler.sendMessage(msg);
        private String inputStream2String(InputStream inputStream)throws IOException{
            final int bufferSize=1024;
            final char[]buffer=new char[bufferSize];
            final StringBuilder out=new StringBuilder();
            Reader in=new InputStreamReader(InputStream,"utf-8");
            while (true){
                int rsz=in.read(buffer,0,buffer.length);
                if(rsz<0)
                    break;
                out.append(buffer,0,rsz);

            }
            return out.toString();
        }

    }
}